package yt.kpool.activity.profit;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.web.ProfitWebAc;
import yt.kpool.bean.ProfitBean;
import yt.kpool.util.TitleControl;
import yt.kpool.util.Util;

public class ProfitDetailAc extends AppCompatActivity {
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_pricec)
    TextView tv_pricec;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_sl)
    TextView tv_sl;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_jy)
    TextView tv_jy;
    @BindView(R.id.ly_jy)
    LinearLayout ly_jy;
    @BindView(R.id.tv_line)
    TextView tv_line;
    @BindView(R.id.iv_copy1)
    ImageView iv_copy1;
    @BindView(R.id.iv_copy2)
    ImageView iv_copy2;
    private ProfitBean.DataBean dataBean;
    private String Dw;
    public static void start(Context mActiivty, ProfitBean.DataBean dataBean,String Dw) {
        Intent intent = new Intent(mActiivty, ProfitDetailAc.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("dataBean", dataBean);
        intent.putExtra("Dw", Dw);
        intent.putExtras(bundle);
        mActiivty.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profitdetail);
        ButterKnife.bind(this);
        initTitle();
        getDataBean();
        init();

    }

    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("收益记录");

    }

    private void init()
    {
        NumberFormat format=NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(20);
        tv_price.setText(format.format(dataBean.getAmount())+"");
        tv_pricec.setText(Dw);
        tv_date.setText(dataBean.getProfitTime());
        tv_sl.setText(dataBean.getHashrate24H());
        tv_address.setText(dataBean.getAddress());
        if(dataBean.getHash()==null||dataBean.getHash().length()==0)
        {
            ly_jy.setVisibility(View.GONE);
            tv_line.setVisibility(View.GONE);
        }
        else
        {
            tv_jy.setText(dataBean.getHash());

        }


    }
    private void getDataBean() {
        dataBean = (ProfitBean.DataBean) getIntent().getSerializableExtra("dataBean");
        Dw=getIntent().getStringExtra("Dw");
    }

    @OnClick({R.id.tv_address,R.id.tv_jy,R.id.iv_copy1,R.id.iv_copy2})
    public void onClick(View view)
    {
        switch (view.getId())
        {

            case R.id.tv_address:
                //地址
                if(dataBean.getAddressUrl()==null||dataBean.getAddressUrl().equals(""))
                {
                    Toast.makeText(this,"没有找到区域地址",Toast.LENGTH_SHORT).show();
                    return;
                }
                ProfitWebAc.start(ProfitDetailAc.this,dataBean.getAddressUrl(),"区块浏览器");
                break;
            case R.id.tv_jy:
                //交易
                if(dataBean.getHashUrl()==null||dataBean.getHashUrl().equals(""))
                {
                    Toast.makeText(this,"没有找到区域地址",Toast.LENGTH_SHORT).show();
                    return;
                }
                ProfitWebAc.start(ProfitDetailAc.this,dataBean.getHashUrl(),"区块浏览器");
                break;
            case R.id.iv_copy1:
                Fz(dataBean.getAddress());
                break;
            case R.id.iv_copy2:
                Fz(dataBean.getHash());
                break;
        }

    }

    private void Fz(String txt)
    {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(txt);
        Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();

    }
}
